const app = getApp()
const WXAPI = require("../../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ls:[{},{},{}],
    info:{},
    id: '',
    astatus: '',
    isPassshow: false, //密码弹窗
    isFocus: true, //是否自动聚焦
    Length: 6, //密码长度
    entryList: "",
    active: 0,
    orderId: '', //订单id
    isResult: false, //是否设置密码
    shareImg:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight:app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom
    })
    this.data.id = options.id
    this.data.astatus = options.status
    console.log(this.data.id,this.data.astatus)
    this.setData({
      astatus: this.data.astatus
    })
    if(this.data.astatus==5){
      wx.hideShareMenu()
    }
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //分享
  onShareAppMessage: function () {
    return {
      title: '送你蜜链盟资产，点击领取',
      path: `/userPageA/pages/scan/receiving_assets/receiving_assets?id=` + this.data.id,
      imageUrl: this.data.shareImg
    }
  },
  getInfo(){ //sendGive
    wx.showLoading({
      title: '',
    })
    var data = {
      id: this.data.id,
    }
    WXAPI.sendGive(data, (res) => {
      const data = res.result
      if (res.code == 200) {
        this.data.info = res.result
        this.setData({
          info:res.result
        })
        this.canvasdraw()
        wx.hideLoading()
      }
    })
  },
  canvasdraw: function (canvas) {
    console.log(1111111)
    var that = this;
    
    const ctx = wx.createCanvasContext('canvas')
    ctx.save()
    var path = "/images/canvas_bj.png";
    ctx.drawImage(path, 0, 0, 310, 247);
    ctx.restore()
    ctx.save()
    var str = '送你'
    // ctx.font = 'normal bold 20px sans-serif';
    ctx.setTextAlign('center'); // 文字居中
    ctx.setFillStyle("#FFF0D3");
    ctx.setFontSize(40)
    ctx.fillText(str, 310/2,65)
    // ctx.draw()
    
    var str2 = '￥' + this.data.info.giveTotal
    ctx.setFontSize(40)
    ctx.setFillStyle("#FFF0D3");
    ctx.setTextAlign('center');
    ctx.fillText(str2, 310/2,125)
    ctx.restore()
    ctx.save()
    var path2 = "/images/canvas_djlq.png";
    ctx.drawImage(path2, 46, 150, 220, 54);
    ctx.restore()
    ctx.draw()
    setTimeout(()=>{
      this.daochu()
    },500)
    
  },
  daochu(){
    console.log(1111)
    var _this = this
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 310,
      height: 247,
      destWidth: 620,
      destHeight: 494,
      fileType: 'jpg', //图片的质量，目前仅对 jpg 有效。取值范围为 (0, 1]，不在范围内时当作 1.0 处理。
      quality: 1,
      canvasId: 'canvas',
      success(res) {
        console.log(222)
        console.log(res.tempFilePath)
        _this.data.shareImg = res.tempFilePath
        _this.setData({
          img: res.tempFilePath
        })
      }
    })
  },
  copy(){ //复制
    wx.setClipboardData({
      data: this.data.info.orderNo,
      success (res) {
        wx.showToast({
          title: '复制成功',
        })
      }
    })    
  },
  //点击保存图片
 save () {
  let that = this
  //若二维码未加载完毕，加个动画提高用户体验
  wx.showToast({
   icon: 'loading',
   title: '正在保存图片',
   duration: 1000
  })
  //判断用户是否授权"保存到相册"
  wx.getSetting({
   success (res) {
    //没有权限，发起授权
    if (!res.authSetting['scope.writePhotosAlbum']) {
     wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success () {//用户允许授权，保存图片到相册
       that.savePhoto();
      },
      fail () {//用户点击拒绝授权，跳转到设置页，引导用户授权
       wx.openSetting({
        success () {
         wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
           that.savePhoto();
          }
         })
        }
       })
      }
     })
    } else {//用户已授权，保存到相册
     that.savePhoto()
    }
   }
  })
 },
//保存图片到相册，提示保存成功
 savePhoto() {
    let that = this
    let img = that.data.info.sendQrCode
    // console.log(that.data.info.sendQrCode)
    // console.log(this.data.Imgurl)
    var save = wx.getFileSystemManager();
    var number = Math.random();
    save.writeFile({
      filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png',
      data: img.slice(22),
      encoding: 'base64',
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png',
          success: function (res) {
            wx.showToast({
              title: '保存成功',
            })
          },
          fail: function (err) {
            console.log(err)
          }
        })
        console.log(res)
      }, fail: err => {
        console.log(err)
      }
    })
  },
  getcheckPayPsw(){ //判断是否设置支付密码
    let result = false
    var data = {
      
    }
    WXAPI.checkPayPsw(data, (res) => {
      if (res.code == 200) {
        this.setData({
          isPassshow: true
        })
      }else{
        wx.showModal({
          title: '',
          content: '未设置支付密码，不可支付',
          cancelText: '放弃',
          cancelColor:'#B1B1B1',
          confirmText:'去设置',
          confirmColor:'#FF5800',
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
              // _this.setData({
              //   isPassshow: false
              // })
            }
          }
        })
      }
    })
  },
  inputFocus() { //点击聚焦
    this.setData({
        isFocus: true
    })
  },
  inputValue(e) { //监听密码输入
    var value = e.detail.value;
    var list= e.detail.value.split('')
    this.setData({
        entryList: value,
        active: list.length
    })
    console.log(list.length)
    console.log(value)
    console.log(this.data.entryList)
    if(list.length>=6){
      wx.showLoading({
        title: '支付中',
      })
       var data = {
        orderId: this.data.info.orderId, //订单id
        payPwd: value, //支付密码
      }
      WXAPI.pay(data, (res) => {
        if (res.code == 200) {
          let info = JSON.stringify(res.result)
          wx.showToast({
            title: '支付成功',
          })
          setTimeout(()=>{
            this.getInfo()
            this.setData({
              isPassshow: false,
              astatus: 3
            })
            
          },1000)
          wx.hideLoading({})
        }
      })
      
    }
  },
  closePay(){ //关闭密码弹窗
    let _this = this
    wx.showModal({
      title: '',
      content: '是否放弃本次付款',
      cancelText: '放弃',
      cancelColor:'#B1B1B1',
      confirmText:'继续付款',
      confirmColor:'#FF5800',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
          _this.setData({
            isPassshow: false
          })
        }
      }
    })
  },
  back(){
    wx.navigateBack({
      delta: 0,
    })
  },
  //忘记密码
  forget(){
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/changePayPass/changePayPass',
    })
  },
})